package edu.cmu.scs.hci.TagIt;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class TagItMain extends ListActivity {
	
	private static final int ADD_TAG_ID = 0;
	
	private PhotosDbAdapter mPhotoDb;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPhotoDb = new PhotosDbAdapter(this);
        mPhotoDb.open();
        Cursor c = mPhotoDb.selectPhotos(20);
        setContentView(R.layout.main);
        setListAdapter(new PhotoCursorAdapter(this, c));
        registerForContextMenu(getListView());
    }
    
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
    	super.onCreateContextMenu(menu, v, menuInfo);
    	menu.add(0, ADD_TAG_ID, 0, R.string.add_tag);
    }
    
    @Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ADD_TAG_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			long row_id = info.id;
			Intent i = new Intent(this, SimpleTagger.class);
			Bundle extras = new Bundle();
			extras.putLong(PhotosDbAdapter.KEY_ID, row_id);
			i.putExtras(extras);
			startActivity(i);
			return true;
		}
    	return super.onContextItemSelected(item);
    }
}